package com.orange.utils;


import com.orange.commons.Constant;
import com.orange.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 获取请求、响应、会话对象,获取当前用户
 * @author Emp
 * @projectName: crm
 * @package: com.powernode.utils
 * @className: WebUtil
 * @date: 2021/11/29 14:05
 * @version: 1.0
 */
public class WebMvcUtil {


    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    public static HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getResponse();
    }

    public static HttpSession getSession(){
       return getRequest().getSession();
    }

    public static ServletContext getContext(){
        return getRequest().getServletContext();
    }

    /**
     * 获取当前用户
     */
    public static User getCurrentUser(){
        User user = (User) getSession().getAttribute(Constant.SESSION_USER_KEY);
        return user;
    }

    /**
     * 获取当前用户名
     */
    public static String getCurrentUserName(){
       return getCurrentUser().getUsername();
    }
}
